package com.testing.sunnycommon.controller;

import com.testing.sunnycommon.annotations.SunnyAdvice;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.SunnyModule;
import com.testing.sunnycommon.model.SunnyProject;
import com.testing.sunnycommon.service.SunnyProjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SunnyProjectController
 * @Description 类型说明
 * @Date 2023/5/8 21:21
 * @Created by 特斯汀Roy
 */
@RestController
@RequestMapping("/sunnyProject")
public class SunnyProjectController {

    @Autowired
    SunnyProjectService sunnyProjectService;

    @GetMapping
    public Result<List<SunnyProject>> listSunnyProjects() {
        return Result.success(sunnyProjectService.list());
    }

    @GetMapping("/{projectId}/modules")
    public Result<List<SunnyModule>> listSunnyModulesInProject(@PathVariable Long projectId) {
        if (sunnyProjectService.getById(projectId) == null) {
            return Result.fail(1001, "查询项目中的模块失败", new ArrayList<>());
        }
        return Result.success(sunnyProjectService.listModuleInProject(projectId));
    }

    @ApiOperation("根据projectId查询单个项目的信息")
    @GetMapping("/{projectId}")
    public Result<SunnyProject> getOneSunnyProject(@PathVariable Long projectId) {
        SunnyProject byId = sunnyProjectService.getById(projectId);
        if (byId == null) {
            return Result.fail(1002, "查询不到指定的项目", byId);
        } else {
            return Result.success(byId);
        }
    }

    @SunnyAdvice("创建自动化测试项目")
    @PostMapping
    public Result addSunnyProject(@RequestBody SunnyProject project) {
        //处理可能通过接口传参的时候，设置的step和case数量，都重置为0
        project.setCaseCount(0L);
        project.setStepCount(0L);
        boolean save = sunnyProjectService.save(project);
        if (save) {
            return Result.success("新增项目【" + project.getName() + "】成功", project);
        } else {
            return Result.fail(1010, "新增失败", project.toString());
        }
    }


    @SunnyAdvice("修改自动化项目")
    @PutMapping
    public Result updateSunnyProject(@RequestBody SunnyProject project) {
        SunnyProject oldProject = sunnyProjectService.getById(project.getId());
        boolean result = sunnyProjectService.updateSunnyProject(project);
        if (result) {
            return Result.success("将【" + oldProject.getName() + "】项目名称改为了" + project.getName(), sunnyProjectService.getById(project.getId()));
        } else {
            return Result.fail(1020, "修改项目失败", project);
        }
    }

    /**
     * 一次只删一条项目记录
     */
    @SunnyAdvice("删除项目")
    @DeleteMapping("/{projectId}")
    public Result deleteSunnyProject(@PathVariable Long projectId) {
        SunnyProject project = sunnyProjectService.getById(projectId);
        boolean isRemoveOK = sunnyProjectService.removeById(projectId);
        if (isRemoveOK) {
            return Result.success("删除项目【" + project.getName() + "】成功", project);
        } else {
            return Result.fail(1030, "删除项目【" + project.getName() + "】失败", project);
        }
    }


}
